//
//  AppInfo+Device.m
//  Project
//
//  Created by Suny on 14-8-22.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "AppInfo+Device.h"
#import "AppInfo+System.h"

@implementation AppInfo (Device)
+ (NSString *)deviceModel
{
	
	return [UIDevice currentDevice].model;
	
}

+ (NSString *)deviceUUID
{
	
	Class openUDID = NSClassFromString( @"OpenUDID" );
	if ( openUDID )
	{
        if ([openUDID respondsToSelector:@selector(value)])
        {
            return @"";
        }
    }
	
	
	return nil;
}


+ (BOOL)isDevicePhone
{
	
	NSString * deviceType = [UIDevice currentDevice].model;
	
	if ( [deviceType rangeOfString:@"iPhone" options:NSCaseInsensitiveSearch].length > 0 ||
		[deviceType rangeOfString:@"iPod" options:NSCaseInsensitiveSearch].length > 0 ||
		[deviceType rangeOfString:@"iTouch" options:NSCaseInsensitiveSearch].length > 0 )
	{
		return YES;
	}
	
	return NO;
}

+ (BOOL)isDevicePad
{
	
	NSString * deviceType = [UIDevice currentDevice].model;
	
	if ( [deviceType rangeOfString:@"iPad" options:NSCaseInsensitiveSearch].length > 0 )
	{
		return YES;
	}
	
	return NO;
}


+ (BOOL)isSimulator
{

    return [[[UIDevice currentDevice] name] hasSuffix:@"Simulator"];
}

+ (BOOL)isPhone
{
	if ( [self isPhone35] || [self isPhoneRetina35] || [self isPhoneRetina4] )
	{
		return YES;
	}
	
	return NO;
}

+ (BOOL)isPhone35
{
	
	if ( [self isDevicePad] )
	{
		if ( [self requiresPhoneOS] && [self isPad] )
		{
			return YES;
		}
		
		return NO;
	}
	else
	{
		return [AppInfo isScreenSize:CGSizeMake(320, 480)];
	}
	
}

+ (BOOL)isPhoneRetina35
{
	
	if ( [self isDevicePad] )
	{
		if ( [self requiresPhoneOS] && [self isPadRetina] )
		{
			return YES;
		}
		
		return NO;
	}
	else
	{
		return [AppInfo isScreenSize:CGSizeMake(640, 960)];
	}
}

+ (BOOL)isPhoneRetina4
{
	
	if ( [self isDevicePad] )
	{
		return NO;
	}
	else
	{
		return [AppInfo isScreenSize:CGSizeMake(640, 1136)];
	}
}

+ (BOOL)isPad
{
	return [AppInfo isScreenSize:CGSizeMake(768, 1024)];
}

+ (BOOL)isPadRetina
{
	return [AppInfo isScreenSize:CGSizeMake(1536, 2048)];
}

+ (BOOL)isScreenSize:(CGSize)size
{
	if ( [UIScreen instancesRespondToSelector:@selector(currentMode)] )
	{
		CGSize size2 = CGSizeMake( size.height, size.width );
		CGSize screenSize = [UIScreen mainScreen].currentMode.size;
		
		if ( CGSizeEqualToSize(size, screenSize) || CGSizeEqualToSize(size2, screenSize) )
		{
			return YES;
		}
	}
	
	return NO;
	
}



static const char * __jb_app = NULL;

+ (BOOL)isJailBroken NS_AVAILABLE_IOS(4_0)
{
	
	static const char * __jb_apps[] =
	{
		"/Application/Cydia.app",
		"/Application/limera1n.app",
		"/Application/greenpois0n.app",
		"/Application/blackra1n.app",
		"/Application/blacksn0w.app",
		"/Application/redsn0w.app",
		NULL
	};
	
	__jb_app = NULL;
	
	// method 1
    for ( int i = 0; __jb_apps[i]; ++i )
    {
        if ( [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:__jb_apps[i]]] )
        {
			__jb_app = __jb_apps[i];
			return YES;
        }
    }
	
    // method 2
	if ( [[NSFileManager defaultManager] fileExistsAtPath:@"/private/var/lib/apt/"] )
	{
		return YES;
	}
	
	// method 3
	if ( 0 == system("ls") )
	{
		return YES;
	}
	
    return NO;
}

+ (NSString *)jailBreaker NS_AVAILABLE_IOS(4_0)
{
	if ( __jb_app )
	{
		return [NSString stringWithUTF8String:__jb_app];
	}
	
	return @"";
}





@end
